<template>
    <div>
        <el-row>
            <el-col>
                <el-form label-width="120px" :model="FinanceReimbursementDB" :rules="rules" ref="FinanceReimbursementDBrefs">
                    <el-row>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="报销类别" prop="FinanceType">
                                <el-select width="100%" v-model="FinanceReimbursementDB.FinanceType" filterable @change="FinanceTypeNumber" placeholder="请选择类型" style="width:100%">
                                    <el-option label="常规报销" :value="4"></el-option>
                                    <el-option label="差旅报销" :value="8"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :offset="2" :span="9">
                            <el-form-item label="申请人">
                                <el-select width="100%" v-model="FinanceReimbursementDB.Borrower" filterable placeholder="请选择申请人" style="width:100%">
                                    <el-option value="">申请人</el-option>
                                    <el-option v-for="(v,k) in AllUsers" :value="v.Id" :data-userid="v.Id" :label="v.UserName" v-text="v.UserName" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="部门">
                                <el-select v-model="FinanceReimbursementDB.Department" filterable placeholder="请选择部门" style="width:100%">
                                    <el-option value="">请选择部门</el-option>
                                    <el-option value=" ">全部</el-option>
                                    <el-option v-for="(v,k) in AllDepartment" :value="v.Id" :data-userid="v.Id" :label="v.DepartmentName" v-text="v.DepartmentName" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="项目">
                                <el-select v-model="FinanceReimbursementDB.Project" filterable placeholder="请选择项目" style="width: 100%">
                                    <el-option value="">请选择项目</el-option>
                                    <el-option :value="0" label="无"></el-option>
                                    <el-option v-for="(v,k) in AllProject" :value="v.Id" :data-userid="v.Id" :label="v.ProjectName" v-text="v.ProjectName" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <div class="Two" :style="objcss">
                        <el-row>
                            <el-col :offset="2" :span="9">
                                <el-form-item label="出差类型">
                                    <el-select v-model="FinanceReimbursementDB.TravelType" filterable placeholder="请选择部门" style="width:100%">
                                        <el-option value="">请选择</el-option>
                                        <el-option value="1" label="项目驻扎"></el-option>
                                        <el-option value="2" label="非项目驻扎"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :offset="2" :span="9">
                                <el-form-item label="出差人">
                                    <el-select v-model="FinanceReimbursementDB.TravelPeople" filterable placeholder="请选择项目" style="width: 100%">
                                        <el-option v-for="(v,k) in AllUsers" :value="v.Id" :data-userid="v.Id" :label="v.UserName" v-text="v.UserName" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>


                        <el-row>
                            <el-col :offset="2" :span="9">
                                <el-form-item label="出差地点">
                                    <el-input typeof="text" v-model="FinanceReimbursementDB.TravelAddress"></el-input>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :offset="2" :span="9">
                                <el-form-item label="开始时间">
                                    <el-date-picker v-model="FinanceReimbursementDB.TravelStartTime"
                                                    type="date"
                                                    placeholder="开始时间">
                                    </el-date-picker>

                                </el-form-item>
                            </el-col>
                            <el-col :offset="2" :span="9">
                                <el-form-item label="结束时间">
                                    <el-date-picker v-model="FinanceReimbursementDB.TravelEndTime"
                                                    type="date"
                                                    placeholder="开始时间">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>

                    <el-row>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="收款人">
                                <el-cascader v-model="editPayeeArray"
                                             placeholder="收款人"
                                             :options="GetPayArray"
                                             filterable></el-cascader>
                            </el-form-item>
                        </el-col>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="金额">
                                <el-input v-model="FinanceReimbursementDB.Money" readonly></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="20" :offset="2">
                            <el-form-item label="明细">
                                <el-table :data="detailed"
                                          border
                                          style="width: 100%;">
                                    <el-table-column prop="CategoryOne"
                                                     label="分类一"
                                                     header-align="center"
                                                     align="center"
                                                     style="width: 100%;text-align:center;padding:0px">
                                        <template slot-scope="scope">
                                            <label>{{GetNameById(scope.row.CategoryOne)}}</label>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="CategoryTwo"
                                                     label="分类二"
                                                     header-align="center"
                                                     align="center"
                                                     style="width: 100%;text-align:center;padding:0px">
                                        <template slot-scope="scope">
                                            <label>{{GetNameById(scope.row.CategoryTwo)}}</label>
                                        </template>

                                    </el-table-column>
                                    <el-table-column prop="Money"
                                                     header-align="center"
                                                     align="center"
                                                     label="金额"
                                                     style="width: 100%;text-align:center;padding:0px">
                                    </el-table-column>
                                    <el-table-column prop="Purpose"
                                                     header-align="center"
                                                     align="center"
                                                     label="用途"
                                                     style="width: 100%;text-align:center;padding:0px">
                                    </el-table-column>
                                    <el-table-column prop="address"
                                                     header-align="center"
                                                     label="操作"
                                                     align="center"
                                                     width="200px"
                                                     style=" text-align:center;padding:0px">
                                        <template slot-scope="scope">
                                            <el-button type="success" class="BtnSize" size="mini" @click="GetFinance(scope.row)" plain>修改</el-button>
                                            <el-button type="danger" class="BtnSize" size="mini" @click="DelAddLoandetails(scope.row,scope.$index)" plain>删除</el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="20" :offset="2">
                            <el-form-item label="单据冲账完毕">
                                <el-checkbox v-model="NoChecked" label="冲账完毕" border size="medium"></el-checkbox>
                                <span style="color:red;">温馨提示：如果后续还需要对此单据进行冲账操作，请取消勾选</span>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="20" :offset="2">
                            <el-form-item label="备注">
                                <el-input v-model="FinanceReimbursementDB.Remark"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="20" :offset="2">
                            <el-form-item label="图片上传">
                                <!--<el-image :src="Device_details.imgUrl"></el-image>-->
                                <upload-file ref="upload_file_refs" style="display: inline-block;margin-right: 10px;display: block"
                                             content="图片选择"
                                             list_type="picture-card"
                                             btnsize=""
                                             :another-data="AnotherData"
                                             :showfilelist="true"></upload-file>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row :gutter="20" style="text-align: center">
                        <input type="hidden" id="Id" />
                        <el-button type="primary" @click="AddFinanceLoandetailsShow_event">添加明细</el-button>
                        <el-button type="success" @click="InsertFinance()">确定</el-button>
                    </el-row>
                </el-form>
            </el-col>
        </el-row>
        <div>
            <el-dialog :visible.sync="AddFinanceLoandetailsShow" width="60%" title="新建单据明细" :append-to-body="true">
                <AddFinanceLoandetails></AddFinanceLoandetails>
            </el-dialog>

            <el-dialog :visible.sync="UpFinanceLoandetailsShow" width="60%" title="修改单据明细" :append-to-body="true">
                <UpFinanceLoandetails :form="form"></UpFinanceLoandetails>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    //import { IsExist } from "/public/Scriput/IsExist"
    import UploadFile from "../../UploadFile";
    import AddFinanceLoandetails from "./FinanceLoandetails/AddFinanceLoandetails"
    import UpFinanceLoandetails from "./FinanceLoandetails/UpFinanceLoandetails"

    export default {
        name: "AddFinanceReimbursement",
        components: {
            'AddFinanceLoandetails': AddFinanceLoandetails,
            'UpFinanceLoandetails': UpFinanceLoandetails,
            "upload-file": UploadFile,
        },
        props: {
            GetPayArray: {
                type: Array,
                required: true
            },
            AllUsers: {
                type: Array,
                required: true
            },
            AllProject: {
                type: Array,
                required: true
            },
            AllDepartment: {
                type: Array,
                required: true
            },
            FinanceReimbursementDB: {
                type: Object,
                required: true
            },
            AllCategory: {
                type: Array,
                required: true
            },
            editPayee: {
                type: Array,
                required: true
            },
        },

        watch: {
            editPayee: function (val, oval) {
                this.editPayeeArray = val
            },
            FinanceReimbursementDB: function (val) {
                val.FinanceType = '';
                this.detailed = val.FinanceDocDetails;
            }
        },
        data() {
            return {
                editPayeeArray: [],
                objcss: {
                    display: "none",
                },
                AddFinanceLoandetailsShow: false,
                UpFinanceLoandetailsShow: false,
                src: '',
                bInt: 0,
                detailed: [
                ],
                form: {
                    Purpose: "",
                    Money: '',
                    CategoryTwo: '',
                    CategoryOne: '',
                    HasInvoice: false,
                },
                FinanceType: '',
                NoChecked: true,
                //文件上传参数
                AnotherData: {
                    FileType: 512,
                    id: 0,
                    Type: 0,
                    Single: false,
                },
                rules:{
                    FinanceType:[ { required: true, message: '请选择报销类型', trigger: 'change' } ],
                },
            }
        },

        created() {
            this.detailed = this.FinanceReimbursementDB.FinanceDocDetails;
            this.FinanceReimbursementDB.FinanceType = '';
            this.editPayeeArray = this.editPayee;
        },
        methods: {
            GetNameById(id) {
                for (var i = 0; i < this.AllCategory.length; i++) {
                    let Id = parseInt(id)
                    if (Id == this.AllCategory[i].Id) {
                        return this.AllCategory[i].CategoryName;
                    }
                }
            },
            AddFinanceLoandetailsShow_event() {
                this.AddFinanceLoandetailsShow = true;
            },
            _GetAddAllMoney() {
                let sum = 0;
                for (var i = 0; i < this.detailed.length; i++) {
                    sum += parseFloat(this.detailed[i].Money);
                }
                this.FinanceReimbursementDB.Money = sum;
            },
            DelAddLoandetails(row, index) {
                this.detailed.splice(index, 1)
                this._GetAddAllMoney();
            },
            GetFinance(row) {
                this.form = row;
                this.UpFinanceLoandetailsShow = true;
            },
            InsertFinance() {
                this.$refs['FinanceReimbursementDBrefs'].validate(vali=>{
                    if (vali){
                        if (this.FinanceReimbursementDB.FinanceType != 2) {
                            if (this.NoChecked) {
                                this.$confirm('请再次确认是否为最后一次冲账，点击确定按钮后，后续将不能再对此单据进行冲账！', '警告，请您仔细阅读', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消'
                                }).then(() => {
                                    this.SaveFinance();
                                })
                            } else {
                                this.SaveFinance();
                            }
                        } else {
                            this.$message.info("请选择报销类型");
                        }
                    }
                })
            },
            SaveFinance() {
                this.FinanceReimbursementDB.PayeeType = this.editPayeeArray[0];
                this.FinanceReimbursementDB.Payee = this.editPayeeArray[1];
                var ReimbursementDB = {};
                ReimbursementDB.PayeeType = this.FinanceReimbursementDB.PayeeType;//付款类型
                ReimbursementDB.Payee = this.FinanceReimbursementDB.Payee;//付款人Id
                ReimbursementDB.FinanceType = this.FinanceReimbursementDB.FinanceType;//报销类型
                ReimbursementDB.Borrower = this.FinanceReimbursementDB.Borrower;
                ReimbursementDB.Department = this.FinanceReimbursementDB.Department;
                ReimbursementDB.Project = this.FinanceReimbursementDB.Project;
                ReimbursementDB.TravelType = this.FinanceReimbursementDB.TravelType;
                ReimbursementDB.TravelPeople = this.FinanceReimbursementDB.TravelPeople;
                ReimbursementDB.TravelAddress = this.FinanceReimbursementDB.TravelAddress;
                ReimbursementDB.TravelStartTime = this.FinanceReimbursementDB.TravelStartTime;
                ReimbursementDB.TravelEndTime = this.FinanceReimbursementDB.TravelEndTime;
                ReimbursementDB.Money = this.FinanceReimbursementDB.Money;
                ReimbursementDB.Remark = this.FinanceReimbursementDB.Remark;
                ReimbursementDB.LoanNumber = this.FinanceReimbursementDB.LoanNumber;
                var financeDocDetails = this.DeleteNullArr(this.detailed);
                var _this = this;
                let loadding = this.openLoading();
                this.$http.post("/Finance/AddFinanceReimbursement",
                    { finance: ReimbursementDB, financeDocDetails: financeDocDetails, IsFinishReversal: this.NoChecked },
                ).then(list => {
                    loadding.close();
                    var result = list.data
                    if (result.Id != 0) {
                        if (this.$refs['upload_file_refs'].fileList.length > 0) {
                            this.AnotherData.id = result.Id;
                            this.$refs['upload_file_refs'].submitUpload()
                        }
                        this.$message.success("借款单添加成功");
                        _this.$parent.$parent.AddFinanceLoanShow = false;
                        _this.$parent.$parent._GetFinanceLoanDetail();
                    }
                    else {
                        this.$message.info("添加失败");
                    }
                }).catch((res) => {
                    loadding.close();
                    this.$message.info('服务器异常,请联系管理员!')
                })
            },

            Changepayee(obj) {
                console.log(obj)
            },
            //去掉空数组
            DeleteNullArr(data) {
                var arr = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].length != 0) {
                        if (data[i].CategoryTwo == "") {
                            data[i].CategoryTwo = 0;
                        }
                        arr.push(data[i]);
                    }
                }
                return arr;
            },
            FinanceTypeNumber(val) {
                if (val == 4) {
                    this.objcss.display = "none";
                }
                else {
                    this.objcss.display = "block";
                }
            },
        },
    }
</script>

<style scoped>
    .el-table th {
        padding: 0px;
    }

    .el-select-dropdown__item {
        width: 270px !important;
    }
</style>
